package com.java.common.lang.domain;

import com.java.common.constant.HttpStatus;

/**
 * 响应包装
 *
 * @author yilabao
 * @date 2021年1月23日
 */
public class ReturnVO<T> {

    private static final String ERROR_MSG = "操作失败";

    private static final String SUCCESS_MSG = "操作成功";

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 数据对象
     */
    private T data;

    /**
     * 说明
     */
    private String msg;

    public ReturnVO() {
    }

    public ReturnVO(T data) {
        this(HttpStatus.SUCCESS, SUCCESS_MSG, data);
    }

    public ReturnVO(Integer code, String msg) {
        this(code, msg, null);
    }

    public ReturnVO(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public static <T> ReturnVO<T> success() {
        return success(SUCCESS_MSG);
    }

    public static <T> ReturnVO<T> success(String msg) {
        return success(msg, null);
    }

    public static <T> ReturnVO<T> success(T data) {
        return success(SUCCESS_MSG, data);
    }

    public static <T> ReturnVO<T> success(String msg, T data) {
        return new ReturnVO<T>(HttpStatus.SUCCESS, msg, data);
    }

    public static <T> ReturnVO<T> error() {
        return error(ERROR_MSG);
    }

    public static <T> ReturnVO<T> error(String msg) {
        return error(msg, null);
    }

    public static <T> ReturnVO<T> error(String msg, T data) {
        return error(HttpStatus.ERROR, msg, data);
    }

    public static <T> ReturnVO<T> error(Integer code, String msg) {
        return error(code, msg, null);
    }

    public static <T> ReturnVO<T> error(Integer code, String msg, T data) {
        return new ReturnVO<T>(code, msg, data);
    }

    @Override
    public String toString() {
        return "ReturnVO [code=" + code + ", data=" + data + ", msg=" + msg + "]";
    }

}
